window.onload = function () {
	let main2 = new Runn();
	main2.headScroll().inputHover().menu()
}
class Runn {
	constructor() {

	}
	//头部滚动条
	headScroll() {
		let $moveul = $(".header-list");
		let $current = 0;
		setInterval(function () {
			$current++;
			tabLi();
		}, 2000)

		function tabLi() {
			if ($current == 3) {
				$moveul.css({
					top: 0
				})
				$current = 1;
				$moveul.animate({
					top: -30 * $current
				})
			} else {
				$moveul.animate({
					top: -30 * $current
				})
			}
		}
		return this;
	}
	//输入框划入划出效果
	inputHover() {
		$("#headersearch").hover(function () {
			$(this).css({
				border: "1px solid red"
			})
		}, function () {
			$(this).css({
				border: "1px solid #e2e2e2"
			})
		})
		return this;
	}
	//二级菜单显示和隐藏
	menu() {
		$("#headermenu").hover(function () {
			$(this).css({
				display: "block"
			})
		}, function () {
			$(this).css({
				display: "none"
			})
		});
		$("#headernav").hover(function () {
			$("#headermenu").css({
				display: "block"
			})
		}, function () {
			$("#headermenu").css({
				display: "none"
			})
		})
		return this;
	}
}

//得到localstorage里面的内容
function getDetails() {
	var storage = localStorage.getItem("detail");
	storage = JSON.parse(storage);
	$(".detail_img").attr("src", storage.data.hd_thumb_url);
	$(".goods_id").html(storage.data.goods_id);
	$(".goods_name h1").html(storage.data.goods_name);
	$(".goods_price span").html("￥" + (storage.data.normal_price / 100).toFixed(2));
}
getDetails();


//放大镜效果
function fangdajing() {
	let $smallImg = $(".small_img");
	let $cover = $(".smallCursor");
	let $box = $(".fangdajing");
	let $bigImg = $(".big_img");
	$(".small_img,.smallCursor").mouseenter(function () {
		$cover.show();
		$box.show();
	});
	$(".small_img,.smallCursor").mouseleave(function () {
		$cover.hide();
		$box.hide();
	})
	$(".small_img,.smallCursor").mousemove(function(e){
		//鼠标在smallImg上的位置
		var e = e || event;
		var _left = e.pageX - $(".detail_wrap").offset().left -20;
		var _top = e.pageY - 280;
		$cover.css({
			left :Math.min($smallImg.width()-$cover.width(),Math.max(0,_left - $cover.width()/2)),
			top:Math.min($smallImg.height()-$cover.height(),Math.max(0,_top - $cover.height()/2))
		})
	//根据缩放比例,求出大图片的移动距离  
	// 	//$box.width():$cover.width() = $bigImg.width():$smallImg.width()  比例
	// 	//大图移动距离 = -cover移动距离*比例(注意：移动方向相反)
		let scale = $smallImg.width()/$box.width();
		$bigImg.css({
			left:-$cover.position().left*scale,//若要获取元素的left和top值，必须调position方法
			top:-$cover.position().top*scale//获取cover的top值
		})
	})

}
fangdajing();
//滑过下方小图效果
$(".mbox b").mouseover(function () {
	$(this).addClass("cursor").siblings().removeClass("cursor");
})
//点击左右侧按钮效果
function slide() {
	var flag = 0;
	$(".prev").click(function () {
		if (flag-- == 0) {
			$(".mbox").animate({
				left: 0
			})
		} else {

		}
	})
	$(".next").click(function () {
		$(".mbox").animate({
			left: -1 * 60
		})
		flag++;
	})
}
slide();
//点击加号增加商品
$(".add").click(function () {
	$(".good_num").html(Number($(".good_num").html()) + 1);
})
//点击减号减少商品数量
$(".sub").click(function () {
	$(".good_num").html(Number($(".good_num").html()) - 1);
	if (Number($(".good_num").html()) - 1 <= 0) {
		$(".good_num").html("0");
	}
})

//点击加入购物车按钮
function addCar(){
	var cart_list = (function () {
		var storage = localStorage.getItem("carts");
		if (storage === null) {
			return [];
		}
		return JSON.parse(storage);
	})();
	var add_callback = $.Callbacks();
	add_callback.add(sumGoodsCount);
	function sumGoodsCount() {
		if(cart_list.length === 0){
			$(".cart-count").html("0");
		}else{
			var total = 0;
			for(var i =0;i<cart_list.length;i++){
				total = total + cart_list[i].count;
			}
			$(".cart-count").html(total);
		}		
	}
	sumGoodsCount();

	$(".add-cart").click(function () {
		var item = localStorage.getItem("detail");
		item = JSON.parse(item);
		item.count = Number($(".good_num").html());
		//console.log(item)
		//在插入之前判断是否有这个数据
		var has_same = cart_list.some(function(cart_item){
			if(item.data.goods_id === cart_item.data.goods_id){
				cart_item.count = item.count + cart_item.count;
				return true;
			}
		})
		if(!has_same){
			cart_list.push(item);
		}
		// cart_list.push(item);
		localStorage.setItem("carts", JSON.stringify(cart_list));
		add_callback.fire();
		alert("加入购物车成功");
	})
}
addCar();